package com.scale.service.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.user.dto.VoicePreferencesUpdateDTO;
import com.example.user.entity.UserPreferences;
import com.example.user.vo.VoicePreferencesVO;
import com.scale.service.user.mapper.UserPreferencesMapper;
import com.scale.service.user.service.UserVoicePreferencesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 用户语音偏好设置服务实现类
 * @author crp
 * @since 2025-09-28
 */
@Slf4j
@Service
public class UserVoicePreferencesServiceImpl implements UserVoicePreferencesService {

    @Autowired
    private UserPreferencesMapper userPreferencesMapper;

    @Override
    public VoicePreferencesVO getVoicePreferences(Long userId) {
        QueryWrapper<UserPreferences> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        
        UserPreferences preferences = userPreferencesMapper.selectOne(queryWrapper);
        
        // 如果用户没有偏好设置，创建默认设置
        if (preferences == null) {
            createDefaultVoicePreferences(userId);
            preferences = userPreferencesMapper.selectOne(queryWrapper);
        }
        
        VoicePreferencesVO vo = new VoicePreferencesVO();
        BeanUtils.copyProperties(preferences, vo);
        
        return vo;
    }

    @Override
    public void updateVoicePreferences(Long userId, VoicePreferencesUpdateDTO updateDTO) {
        QueryWrapper<UserPreferences> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        
        UserPreferences existingPreferences = userPreferencesMapper.selectOne(queryWrapper);
        
        if (existingPreferences == null) {
            // 如果不存在偏好设置，先创建默认设置
            createDefaultVoicePreferences(userId);
            existingPreferences = userPreferencesMapper.selectOne(queryWrapper);
        }
        
        // 更新偏好设置
        UserPreferences updatePreferences = new UserPreferences();
        updatePreferences.setId(existingPreferences.getId());
        
        // 只更新非空字段
        if (updateDTO.getPreferredCharacterType() != null) {
            updatePreferences.setPreferredCharacterType(updateDTO.getPreferredCharacterType());
        }
        if (updateDTO.getVoiceSpeed() != null) {
            updatePreferences.setVoiceSpeed(updateDTO.getVoiceSpeed());
        }
        if (updateDTO.getVoicePitch() != null) {
            updatePreferences.setVoicePitch(updateDTO.getVoicePitch());
        }
        if (updateDTO.getVoiceTone() != null) {
            updatePreferences.setVoiceTone(updateDTO.getVoiceTone());
        }
        if (updateDTO.getVoiceLanguage() != null) {
            updatePreferences.setVoiceLanguage(updateDTO.getVoiceLanguage());
        }
        if (updateDTO.getAutoEmotionDetection() != null) {
            updatePreferences.setAutoEmotionDetection(updateDTO.getAutoEmotionDetection());
        }
        if (updateDTO.getPrivacyLevel() != null) {
            updatePreferences.setPrivacyLevel(updateDTO.getPrivacyLevel());
        }
        if (updateDTO.getNotificationEnabled() != null) {
            updatePreferences.setNotificationEnabled(updateDTO.getNotificationEnabled());
        }
        if (updateDTO.getVoiceNotificationEnabled() != null) {
            updatePreferences.setVoiceNotificationEnabled(updateDTO.getVoiceNotificationEnabled());
        }
        
        updatePreferences.setUpdatedAt(LocalDateTime.now());
        
        int result = userPreferencesMapper.updateById(updatePreferences);
        if (result == 0) {
            throw new RuntimeException("更新语音偏好设置失败");
        }
        
        log.info("用户语音偏好设置更新成功: userId={}", userId);
    }

    @Override
    public void resetVoicePreferences(Long userId) {
        QueryWrapper<UserPreferences> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        
        UserPreferences existingPreferences = userPreferencesMapper.selectOne(queryWrapper);
        
        if (existingPreferences != null) {
            // 重置为默认值
            UserPreferences resetPreferences = new UserPreferences();
            resetPreferences.setId(existingPreferences.getId());
            resetPreferences.setPreferredCharacterType("psychologist");
            resetPreferences.setVoiceSpeed(new BigDecimal("1.00"));
            resetPreferences.setVoicePitch(new BigDecimal("1.00"));
            resetPreferences.setVoiceTone("female");
            resetPreferences.setVoiceLanguage("auto");
            resetPreferences.setAutoEmotionDetection(true);
            resetPreferences.setPrivacyLevel(1);
            resetPreferences.setNotificationEnabled(true);
            resetPreferences.setVoiceNotificationEnabled(true);
            resetPreferences.setUpdatedAt(LocalDateTime.now());
            
            userPreferencesMapper.updateById(resetPreferences);
            log.info("用户语音偏好设置已重置: userId={}", userId);
        } else {
            // 如果不存在，创建默认设置
            createDefaultVoicePreferences(userId);
        }
    }

    @Override
    public void createDefaultVoicePreferences(Long userId) {
        // 检查是否已存在
        QueryWrapper<UserPreferences> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        
        if (userPreferencesMapper.selectCount(queryWrapper) > 0) {
            log.info("用户语音偏好设置已存在，跳过创建: userId={}", userId);
            return;
        }
        
        // 创建默认偏好设置
        UserPreferences preferences = new UserPreferences();
        preferences.setUserId(userId);
        preferences.setPreferredCharacterType("psychologist");
        preferences.setVoiceSpeed(new BigDecimal("1.00"));
        preferences.setVoicePitch(new BigDecimal("1.00"));
        preferences.setVoiceTone("female");
        preferences.setVoiceLanguage("auto");
        preferences.setAutoEmotionDetection(true);
        preferences.setPrivacyLevel(1);
        preferences.setNotificationEnabled(true);
        preferences.setVoiceNotificationEnabled(true);
        preferences.setCreatedAt(LocalDateTime.now());
        preferences.setUpdatedAt(LocalDateTime.now());
        
        int result = userPreferencesMapper.insert(preferences);
        if (result == 0) {
            throw new RuntimeException("创建默认语音偏好设置失败");
        }
        
        log.info("已为用户创建默认语音偏好设置: userId={}", userId);
    }
}

